package com.csqiusheng.zyydt.model

import android.net.Uri
import androidx.lifecycle.MutableLiveData
import com.csqiusheng.base.mvvm.SingleLiveData
import com.csqiusheng.base.utils.FileUtil
import com.csqiusheng.base.utils.showToast
import com.csqiusheng.zyydt.bean.*
import com.csqiusheng.zyydt.http.ApiService
import com.csqiusheng.zyydt.http.RetrofitUtil
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class ApiViewModel : BaseViewModel() {

    val searchList = MutableLiveData<MutableList<Search>>()
    val searchListErrorHttp = SingleLiveData<Any>()
    var search: String? = null
    fun search() {
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).search(search) },
            success = {
                searchList.value = it
            },
            error = {
                searchListErrorHttp.call()
            },

            )
    }

    val provinceList = MutableLiveData<MutableList<Province>>()
    val provinceListErrorHttp = SingleLiveData<Any>()
    fun getProvinceList() {
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).getProvinceList() },
            success = {
                provinceList.value = it
            },
            error = {
                provinceListErrorHttp.call()
            },
        )
    }

    val cityList = MutableLiveData<MutableList<City>>()
    val cityListErrorHttp = SingleLiveData<Any>()
    fun getCityList(id: String) {
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).getCityList(id) },
            success = {
                cityList.value = it
            },
            error = {
                cityListErrorHttp.call()
            },
        )
    }

    val areaList = MutableLiveData<MutableList<Area>>()
    val areaListErrorHttp = SingleLiveData<Any>()
    fun getAreaList(id: String) {
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).getAreaList(id) },
            success = {
                areaList.value = it
            },
            error = {
                areaListErrorHttp.call()
            },
        )
    }

    val schoolList = MutableLiveData<MutableList<School>>()
    val schoolListErrorHttp = SingleLiveData<Any>()
    fun getSchoolList(id: String) {
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).getSchoolList(id) },
            success = {
                schoolList.value = it
            },
            error = {
                schoolListErrorHttp.call()
            },
        )
    }

    val lineFilter = MutableLiveData<LineFilter>()
    val lineFilterErrorHttp = SingleLiveData<Any>()
    fun getLineFilter() {
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).getLineFilter() },
            success = {
                lineFilter.value = it
            },
            error = {
                lineFilterErrorHttp.call()
            },
        )
    }

    var yearProvincialControlBranch: String? = null
    val provincialControlBranch = MutableLiveData<ProvincialControlBranch>()
    val provincialControlBranchErrorHttp = SingleLiveData<Any>()
    fun getProvincialControlBranch() {
        if (yearProvincialControlBranch.isNullOrEmpty()) {
            return
        }
        http(
            api = {
                RetrofitUtil.getRetrofit().create(ApiService::class.java)
                    .getProvincialControlBranch(yearProvincialControlBranch.toString())
            },
            success = {
                provincialControlBranch.value = it
            },
            error = {
                provincialControlBranchErrorHttp.call()
            },
        )
    }

    var yearProvincialControlLine: String? = null
    val provincialControlLine = MutableLiveData<MutableList<ProvincialControlLine>>()
    val provincialControlLineErrorHttp = SingleLiveData<Any>()
    fun getProvincialControlLine() {
        if (yearProvincialControlLine.isNullOrEmpty()) {
            return
        }
        http(
            api = {
                RetrofitUtil.getRetrofit().create(ApiService::class.java)
                    .getProvincialControlLine(yearProvincialControlLine.toString())
            },
            success = {
                provincialControlLine.value = it
            },
            error = {
                provincialControlLineErrorHttp.call()
            },
        )
    }

    val upFile = MutableLiveData<UpFile>()
    fun upFile(uri: Uri) {
        val builder = MultipartBody.Builder().setType(MultipartBody.FORM) //表单类型
        val file = File(FileUtil.getFileAbsolutePath(uri = uri).toString())
        val requestFile: RequestBody = file.asRequestBody("*/*".toMediaTypeOrNull())
        builder.addFormDataPart("file", file.name, requestFile)
        val part = builder.build().part(0)
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).upFile(part) },
            success = {
                upFile.value = it
            },
            error = {
                it.message?.showToast()
            }
        )
    }

    val getRecruitStudentsFilter = MutableLiveData<RecruitStudentsFilter>()

    //    val getRecruitStudentsFilterErrorHttp = SingleLiveData<Any>()
    fun getRecruitStudentsFilter(id: String) {
        http(
            api = {
                RetrofitUtil.getRetrofit().create(ApiService::class.java)
                    .getRecruitStudentsFilter(id)
            },
            success = {
                if (it == null) {
                    getRecruitStudentsFilter.value = RecruitStudentsFilter()
                } else {
                    getRecruitStudentsFilter.value = it
                }

            },
//            error = {
//                getRecruitStudentsFilterErrorHttp.call()
//            },
        )
    }

    var getRecruitStudentsYear: String? = null
    var getRecruitStudentsTypeName: String? = null
    var getRecruitStudentsBatchName: String? = null
    var getRecruitStudentsYearName: String = "年份"
    var getRecruitStudentsTypeNameName: String = "选科"
    var getRecruitStudentsBatchNameName: String = "批次"
    val getRecruitStudentsList = MutableLiveData<MutableList<RecruitStudentsSchool>>()
    val getRecruitStudentsListErrorHttp = SingleLiveData<Any>()
    fun getRecruitStudentsList(id: String) {
        http(
            api = {
                RetrofitUtil.getRetrofit().create(ApiService::class.java)
                    .getRecruitStudentsList(
                        id,
                        getRecruitStudentsYear,
                        getRecruitStudentsTypeName,
                        getRecruitStudentsBatchName
                    )
            },
            success = {
                getRecruitStudentsList.value = it
            },
            error = {
                getRecruitStudentsListErrorHttp.call()
            },
        )
    }

    val getExamNumber = MutableLiveData<String>()
    fun getExamNumber() {
        http(
            api = {
                RetrofitUtil.getRetrofit().create(ApiService::class.java)
                    .getExamNumber()
            },
            success = {
                getExamNumber.value = it
            },

            )
    }

    val getHeadlinesList = MutableLiveData<MutableList<Headlines>>()
    fun getHeadlinesList() {
        http(
            api = {
                RetrofitUtil.getRetrofit().create(ApiService::class.java)
                    .getHeadlinesList()
            },
            success = {
                getHeadlinesList.value = it
            },
        )
    }

    val getNotice = MutableLiveData<Headlines>()
    fun getNotice() {
        http(
            api = {
                RetrofitUtil.getRetrofit().create(ApiService::class.java)
                    .getNotice()
            },
            success = {
                getNotice.value = it
            },
        )
    }

    val provinceModelList = MutableLiveData<ExamYearModel>()
    val provinceListModelErrorHttp = SingleLiveData<Any>()
    fun getProvinceModelList() {
        http(
            api = { RetrofitUtil.getRetrofit().create(ApiService::class.java).getProvinceModelList() },
            success = {
                provinceModelList.value = it
            },
            error = {
                provinceListModelErrorHttp.call()
            },
        )
    }
}